package com.two.service.shopcar.controller;

import cn.hutool.core.date.DateUtil;
import com.two.common.entity.Menu;
import com.two.common.util.CommonResult;
import com.two.service.shopcar.entity.Shopcar;
import com.two.service.shopcar.orderFeign.OrdersFeign;
import com.two.service.shopcar.service.IShopcarService;
import com.two.service.shopcar.vo.RestautantVo;
import com.two.service.shopcar.vo.ShopCarVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 汪瑞明
 * @since 2022-09-01
 */
@RestController
@RequestMapping("/shopcar")
public class ShopcarController {
    @Autowired
    private IShopcarService shopcarService;

    @Autowired
    private OrdersFeign ordersFeign;

    @PostMapping("/payMoney")
    public CommonResult payMoney(@RequestParam String totalPrice, @RequestBody Shopcar[] shopcar){
       return ordersFeign.payMoney(totalPrice,shopcar);
    }
    // 手机购物车提交订单
    @PostMapping("/shopPay")
    public CommonResult shopPay(String totalPrice, @RequestBody RestautantVo[] restautantVo){
        System.out.println("totalPrice==>"+totalPrice);
        System.out.println(restautantVo[0].toString());
        for (int i = 0; i < restautantVo.length; i++) {
            for (int j = 0; j < restautantVo[i].getShopcar().size(); j++) {
                 shopcarService.deleteGoods(restautantVo[i].getShopcar().get(j).getId());
                 //shopcarService.deleteGoods(restautantVo[i].getShopcar().get(j).getId());
            }
        }

        return ordersFeign.shopPay(totalPrice,restautantVo);
    }

    @GetMapping("/goodsNum")
    public CommonResult findGoodsNum(HttpServletRequest request){
        String token = request.getHeader("token");
        return shopcarService.findGoodsNum(token);
    }


    // 更新商品数量
    @PostMapping("/updateById")
    public CommonResult updateById(@RequestBody Shopcar shopcar){
        boolean b = shopcarService.updateById(shopcar);
        if (b){
            return new CommonResult(2000,"修改成功",null);
        }
        return new CommonResult(5000,"修改失败",null);
    }

    // 添加商品到购物车
  /*  @PostMapping("/addGoods")
    public CommonResult addGoods(HttpServletRequest request, @RequestBody ShopCarVo shopCarVo){
        String token = request.getHeader("token");

        return shopcarService.addGoods(token,shopCarVo);
    }*/
  // 添加商品到购物车
    @PostMapping("/addGoods")
    public CommonResult addGoods(@RequestParam String token, @RequestBody Shopcar[] shopcar){
        shopcar[0].setId(DateUtil.format(new Date(), "yyyyMMdd") + System.currentTimeMillis());

        /*System.out.println("======....>>"+shopcar[0].toString());
        String token = request.getHeader("token");*/
        System.out.println("token===>"+token);


        return shopcarService.addGoods(token,shopcar);
    }

    // 查询购物车内的商品并完成分页
    @GetMapping("/findAll")
    public CommonResult findAll(HttpServletRequest request){
        String token = request.getHeader("token");
        return shopcarService.findAll(token);
    }

    // 根据id删除
    @GetMapping("/deleteById")
    public CommonResult deleteById(String id){
        return shopcarService.deleteById(id);
    }

    // 手机端批量删除
    @PostMapping("/deleteBySelect")
    public CommonResult deleteBySelect(@RequestBody RestautantVo[] restautantVo){

        return shopcarService.deleteBySelect(restautantVo);
    }


}
